package com.x.gzcrm.web.controller;


import com.x.gzcrm.model.CURDResult;
import com.x.gzcrm.model.CourseCategory;
import com.x.gzcrm.model.PageResult;
import com.x.gzcrm.model.CourseOrder;
import com.x.gzcrm.service.ICourseCategoryService;
import com.x.gzcrm.service.ICourseOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {

    @Autowired
    ICourseOrderService iCourseOrderService;
    @Autowired
    ICourseCategoryService iCourseCategoryService;

    Logger logger = LoggerFactory.getLogger(CourseOrderController.class);
    //根据id查询数据
    @RequestMapping("detail")
    public String detail(Model model, String order_id){
        CourseOrder order = iCourseOrderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        return "courseorder/detail";
    }
    //删除
    @RequestMapping("delete")
    @ResponseBody
    public CURDResult delete(String order_id){
        CURDResult result = new CURDResult();
        iCourseOrderService.deleteByOrderId(order_id);
        return result;
    }
    //更新
    @RequestMapping("edit")
    public String edit(Model model,String order_id){
        System.out.println("修改定单order_id=" + order_id);
        CourseOrder order = iCourseOrderService.findByOrderId(order_id);
        //读取数据库中的课程类别数据，供页面使用
        List<CourseCategory> categoryList = iCourseCategoryService.findAllList();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("order",order);
        return "courseorder/edit";
    }
    //课程列表
    @RequestMapping("/list")
    public String list(Model model)
    {
        //读取数据库中的课程类别数据，供页面使用
        List<CourseCategory> categoryList = iCourseCategoryService.findAllList();
        model.addAttribute("categoryList",categoryList);
        return "courseorder/list";
    }

    @RequestMapping("listJson")
    @ResponseBody
    /**
     * page 显示当前页数
     * limit 每次显示多少条
     */
    public PageResult<CourseOrder> listJson(CourseOrder condition,int page,int limit)
    {

        //请求参数记录
        logger.info("=====请求参数=====");
        logger.info("condition："+condition);
        logger.info("page："+page);
        logger.info("limit："+limit);



        System.out.println(condition+" "+page +" "+limit);
        PageResult<CourseOrder> result=iCourseOrderService.findPageResult(condition,page,limit);//limit就是pagesize


        //响应数据记录
        logger.info("=====响应数据=====");
        logger.info("result："+result.toString());


        return  result;



    }


    //课程增加
    @RequestMapping("add")
    public String add(Model model)
    {
        //读取数据库中的课程类别数据，供页面使用
        List<CourseCategory> categoryList = iCourseCategoryService.findAllList();
        model.addAttribute("categoryList",categoryList);
        return "courseorder/add";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public CURDResult save(CourseOrder order){
        CURDResult result = new CURDResult();// {success:1,msg:””}
        if(order.getOrder_id() == null || order.getOrder_id().length()==0){
            System.out.println("增加");
            iCourseOrderService.save(order);
        }else{
            System.out.println("更新");
            iCourseOrderService.update(order);
        }

        System.out.println(order);


        return result;
    }
}
